var url_prepend = "";
if(location.pathname.indexOf('consciencepo') > 0 )
  url_prepend = "/consciencepo/"

$('#videos-container').click(function(e) { 
  loadVideo($(e.target));  
});

$(function() {
	$("#video_thumbnails img[rel]").overlay({
		effect: 'apple',
		// when overlay is opened, load our player
		onLoad: function() {
		
		}
		});
});
$('.thumb-up').click(function(e) { 
  voteUp($(e.target));  
});

var FB_UID;
 window.fbAsyncInit = function() {
        FB.Event.subscribe('auth.sessionChange', function(response) {
		  if(response.status == "connected")
		  {
		  	FB_UID = response.session.uid;
		  	$('.thumb-up').removeClass('faded').addClass('button');
		  }
		});
      };




var voteUp = function(target){
	var videoId = target.attr('videoDBId') ;
	var fbId = FB_UID ; 
	if(!videoId || !fbId  )
		return;
		
	var data = {
     'videoId' : videoId ,
     'fbId' : fbId  
   };
   
   $(target).removeClass("thumb-up").addClass("thumb-loader"); 
   
   $.post(
     url_prepend + "index.php/videos/vote_video_up/",
     data,
     function (data, textStatus, xhr) {  
     	$(target).removeClass("thumb-loader").addClass("thumb-up"); 
       	if(data)
       	{
       		$("#video-total-like-" + videoId).html(data); 
       	}
       	else
       	{
          	alert('problem adding comment')
       	}
          
    }); 
    
}

var loadVideo = function(target){
	var videoid = target.attr('videoid') ; 
	if(!videoid   )
		return;
	videoDescription = target.attr('desc') 
	playVideo(videoid, videoDescription);
}

$(document).ready(function() {
	var thumb = $('.video-thumb');
   if(thumb)
   		swfobject.embedSWF("http://www.youtube.com/v/" + thumb.attr('videoID') + "&enablejsapi=1&playerapiid=player1", "ytVideoPlayer", "400", "300", "8", null, null, { allowScriptAccess: "always" }, { id: "ytVideoPlayer" });
	
});
var ytplayer;
function onYouTubePlayerReady(playerId) { 
   ytplayer = document.getElementById("ytVideoPlayer");   
}

	
	
var playVideo = function(videoID, videoDescription){ 
	if(ytplayer && ytplayer.loadVideoById)
	{  
		ytplayer.loadVideoById(videoID);
		return;
	} 
	swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "&enablejsapi=1&playerapiid=player1", 
		                   "ytVideoPlayer", "420", "224", "8", null, null, { allowScriptAccess: "always" }, { id: "ytVideoPlayer" });
	 
	$('.video_description').html( "<div  >" + $('#'+videoID).html() + "</div>"); 
	
}
